FROM node:lts-alpine AS nodejs
RUN mkdir -p /app
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH

FROM nodejs AS npm-cache 
# install and cache node_modules, excluding cypress
COPY package.json .
COPY package-lock.json .
RUN sed --in-place=.bak -e s/.*cypress.*// package.json
RUN npm install && npm cache clear --force

FROM npm-cache AS app-dist
COPY index.html vite.config.ts .env.production ./
COPY public public/ 
COPY src src/
RUN vite build

FROM nodejs AS prod
COPY --from=app-dist /app/dist/ dist/
COPY --from=app-dist /app/vite.config.ts .
# TODO: nginx to serve static files, not vite
RUN npm install --omit=dev --no-save \
                vite@5.1.1 \
                @vitejs/plugin-vue@4.5.2 \
                @vitejs/plugin-vue-jsx@1.0

## CMD ["vite", "preview", "--port", "8008"]
